/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bra.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.bra.R;
import com.bra.chat.ChatFragment;
import com.bra.ui.base.BaseActivity;
import com.easemob.easeui.ui.EaseChatFragment;
/**
 * 聊天页面
 * 需要fragment的使用{@link #EaseChatFragment}
 */
public class ChatActivity extends BaseActivity  {
	private ImageView activity_back;
	private TextView activity_title;
	public static ChatActivity activityInstance;
     private EaseChatFragment chatFragment;
	   String toChatUsername;
	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_chat, null);
	}
	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
	/*	activity_title = (TextView) findViewById(R.id.activity_title);
		activity_back = (ImageView) findViewById(R.id.activity_back);
		activity_title.setText("聊天");
		*/
		 activityInstance = this;
	        //聊天人或群id
	        toChatUsername = getIntent().getExtras().getString("userId");
	        //可以直接new EaseChatFratFragment使用
	        chatFragment = new ChatFragment();
	        //传入参数
	        chatFragment.setArguments(getIntent().getExtras());
	        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
	}
	@Override
	protected void setListener() {
	//	activity_back.setOnClickListener(click);
		
	}
	private View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
		/*	case R.id.activity_back:
				finish();
				break;	*/			
			default:
				break;
			}
		}
	};
	  @Override
	    protected void onDestroy() {
	        super.onDestroy();
	        activityInstance = null;
	    }
	    
	    @Override
	    protected void onNewIntent(Intent intent) {
	        // 点击notification bar进入聊天页面，保证只有一个聊天页面
	        String username = intent.getStringExtra("userId");
	        if (toChatUsername.equals(username))
	            super.onNewIntent(intent);
	        else {
	            finish();
	            startActivity(intent);
	        }

	    }
	    @Override
	    public void onBackPressed() {
	        chatFragment.onBackPressed();
	    }
	    
	    public String getToChatUsername(){
	        return toChatUsername;
	    }
}	     